package com.tianji.aigc.config.ai;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 工具结果保持器，用来存储tools中得到的结果，请求id 作为key， value为键值对数据
 *
 * @author wc
 * @version 1.0
 */
public class ToolResultHolder {

    private static final Map<String, Map<String, Object>> HANDEL_MAP = new ConcurrentHashMap<>();

    private ToolResultHolder(){

    }

    public static void put(String key, String field, Object result){
        if(key == null || field == null){
            return;
        }
        HANDEL_MAP.computeIfAbsent(key, k->new HashMap<>()).put(field, result);
    }

    public static Object get(String key){
        return key == null ? null : HANDEL_MAP.get(key);
    }

    public static Object get(String key, String field){
        return key == null || field == null ? null :
                Optional.ofNullable(HANDEL_MAP.get(key))
                        .map(map->map.get(field)).orElse(null);

    }

    public static void remove(String key){
        if(key == null){
            return;
        }
        HANDEL_MAP.remove(key);
    }
}
